package com.pur.zsms.storemanage.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.storemanage.dao.IStoreDao;
import com.pur.zsms.storemanage.po.StoreInDetailedPo;
import com.pur.zsms.storemanage.po.StoreInPo;
import com.pur.zsms.storemanage.po.StoreMaterDetailPo;
import com.pur.zsms.storemanage.po.StoreMaterPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedValuePo;
import com.pur.zsms.storemanage.po.StoreOutPo;
import com.pur.zsms.storemanage.po.StorePo;
import com.pur.zsms.storemanage.vo.SStoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreInDetailedVo;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutDetailedVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.storemanage.vo.StoreVo;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;

@Repository("storeDao")
public class StoreDaoImpl extends GenericDao implements IStoreDao {
	@Override
	public PaginationSupport listStore(PageController pageController, StoreVo storeVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.store_house_id,o.store_name,o.store_address,o.store_cust_id,b.shop_name FROM t_store o , t_shop b where b.shop_id =o.store_cust_id");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public PaginationSupport listStoreIn(PageController pageController, StoreInVo storeInVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.store_in_id , a.store_house_id, b.store_name, a.cust_id, c.real_name, a.sup_id, d.sup_companyname, a.remark, a.store_in_way, a.store_in_state,a.ord_id,a.mater_value_all FROM t_store_in a, t_store b, t_user c , t_supplier d WHERE a.store_house_id = b.store_house_id AND a.cust_id = c.user_id AND a.sup_id = d.sup_id");
		if (storeInVo.getCustId() != 0) {
			ql.append(" and  a.cust_id = '" + storeInVo.getCustId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport listStoreInForSupplier(PageController pageController, StoreInVo storeInVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.store_in_id, a.sup_sup_id, (SELECT s.sup_companyname FROM t_supplier s WHERE s.sup_id = a.sup_sup_id ) AS supName, a.store_in_way, a.store_in_state,a.creat_date,a.mater_value_all FROM t_store_in a WHERE a.sup_id = '"
				+ storeInVo.getSupId() + "' AND a.cust_id = 0 ");

		ql.append(" ORDER BY creat_date DESC");

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport listStoreOutForSupplier(PageController pageController, StoreOutVo storeOutVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.store_out_id, a.store_out_way, a.store_out_state, a.creat_date, a.store_out_value, a.ord_id, a.remark, a.cust_id, (SELECT shop_name FROM t_shop s WHERE s.shop_id = a.cust_id),(SELECT su.`sup_companyname` FROM t_supplier su WHERE su.sup_id = a.`sup_sup_id` AND su.sup_id !=0)  FROM t_store_out a WHERE a.sup_id = '"
				+ storeOutVo.getSupId() + "' ORDER BY creat_date DESC ");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public PaginationSupport listStoreInDetailed(PageController pageController, StoreInDetailedVo storeInDetailedVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.store_in_id, a.mater_id, b.mater_name, a.mater_level, a.mater_num, a.id, a.mater_num_need, d.store_in_way, d.store_in_state,b.`unit`,b.`specifications`,b.`brands`,a.`mater_value`,a.`mater_value_unit` FROM t_store_in_detailed a, t_material b, t_store_in d WHERE a.store_in_id = d.store_in_id AND a.mater_id = b.mater_id ");
		if (storeInDetailedVo.getStoreInId() != 0) {
			ql.append(" and  a.store_in_id = '" + storeInDetailedVo.getStoreInId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public PaginationSupport listStoreOutDetailed(PageController pageController, StoreOutDetailedVo storeOutDetailedVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.store_out_id, a.mater_id, b.mater_name, a.mater_num,b.`mater_type`,a.`mater_value_unit`,a.`mater_value`,b.`unit`,b.`specifications`,b.`brands` FROM t_store_out_detailed a, t_material b WHERE a.mater_id = b.mater_id ");
		if (storeOutDetailedVo.getStoreOutId() != 0) {
			ql.append(" and  a.store_out_id = '" + storeOutDetailedVo.getStoreOutId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public PaginationSupport listStoreMater(PageController pageController, StoreMaterVo storeMaterVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.sid,o.store_house_id,o.mater_id,o.mater_level,o.mater_number,a.store_name,b.mater_name FROM t_store_material o, t_store a,t_material b WHERE b.mater_id = o.mater_id and a.store_house_id = o.store_house_id ");

		if (storeMaterVo.getUserId() != 0) {
			ql.append("and a.store_cust_id = '" + storeMaterVo.getUserId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport findSupplierStoreMater(PageController pageController, StoreMaterVo storeMaterVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.sid,o.store_house_id,o.mater_id,o.mater_level,b.brands,b.specifications,o.mater_number,a.store_name,b.mater_name,b.unit,(SELECT SUM(d.mater_value) FROM t_store_material_detail d WHERE d.mater_id = o.`mater_id` AND d.store_house_id =  o.store_house_id ),b.mater_type,( SELECT GROUP_CONCAT('数量:',mater_number,b.unit,',进货价:',mater_value,'￥,进货日期:',mater_day, '<br/>' SEPARATOR ''  ) FROM t_store_material_detail s WHERE s.mater_id =o.`mater_id`  GROUP BY mater_id ) FROM t_store_material o, t_store a,t_material b WHERE b.mater_id = o.mater_id and a.store_house_id = o.store_house_id ");

		ql.append("and o.store_house_id = '" + storeMaterVo.getStoreHouseId() + "'");
		if (storeMaterVo.getMaterType() != 0) {
			ql.append(" and b.mater_type = '" + storeMaterVo.getMaterType() + "'");
		}
		if (StringUtils.isNotBlank(storeMaterVo.getMaterName())) {
			ql.append(" and (b.mater_name like '%" + storeMaterVo.getMaterName().trim() + "%' OR b.`all_spell` LIKE '%" + storeMaterVo.getMaterName()
					+ "%' OR b.`fist_spell` LIKE '%" + storeMaterVo.getMaterName() + "%')");
		}
		if (storeMaterVo.getMaterId() != 0) {
			ql.append(" and b.mater_id = '" + storeMaterVo.getMaterId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport findSupplierStoreMaterForMinStore(PageController pageController, StoreMaterVo storeMaterVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT o.sid,o.store_house_id,o.mater_id,o.mater_level,b.brands,b.specifications,o.mater_number,a.store_name,b.mater_name,b.unit,b.mater_type,( SELECT GROUP_CONCAT('数量:',mater_number,b.unit,',进货价:',mater_value  , '<br/>' SEPARATOR ''  ) FROM t_store_material_detail s WHERE s.mater_id =o.`mater_id`  GROUP BY mater_id )  FROM t_store_material o, t_store a,t_material b WHERE b.mater_id = o.mater_id and a.store_house_id = o.store_house_id ");
		ql.append("and o.store_house_id = '" + storeMaterVo.getStoreHouseId() + "'");
		if (storeMaterVo.getMaterType() != 0) {
			ql.append(" and b.mater_type = '" + storeMaterVo.getMaterType() + "'");
		}

		if (StringUtils.isNotBlank(storeMaterVo.getMaterName())) {
			ql.append(" and (b.mater_name like '%" + storeMaterVo.getMaterName().trim() + "%' OR b.`all_spell` LIKE '%" + storeMaterVo.getMaterName()
					+ "%' OR b.`fist_spell` LIKE '%" + storeMaterVo.getMaterName() + "%')");
		}
		if (storeMaterVo.getMaterId() != 0) {
			ql.append(" and b.mater_id = '" + storeMaterVo.getMaterId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport findMaterByOrderDateAndSupplierMaterType(PageController pageController, StoreMaterVo storeMaterVo)
			throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT os.ord_mater_id, m.mater_name, SUM(os.ord_mater_weight), m.unit, m.brands, m.specifications,m.mater_type FROM t_order_supplier o, t_order_supplier_mater os, t_material m WHERE o.ord_date = '"
				+ storeMaterVo.getDate() + "' AND o.ord_sup_id = '" + storeMaterVo.getSupId() + "' ");
		if (storeMaterVo.getMaterType() != 0) {
			ql.append(" AND m.mater_type ='" + storeMaterVo.getMaterType() + "'");

		}
		ql.append(" AND o.ord_sup_id = os.ord_sup_id AND o.ord_id = os.ord_id AND m.mater_id = os.ord_mater_id GROUP BY os.ord_mater_id order by m.mater_type,m.mater_id ");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport findMaterByOrderDateAndSupplierMaterType3(PageController pageController, StoreMaterVo storeMaterVo)
			throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT m.mater_id, m.mater_name, 0, m.unit, m.brands, m.specifications, m.mater_type FROM t_material m , t_material_level b WHERE b.mater_id = m.mater_id AND b.cust_id=0 AND b.sup_id = '"
				+ storeMaterVo.getSupId() + "' ");
		if (storeMaterVo.getMaterType() != 0) {
			ql.append(" AND m.mater_type ='" + storeMaterVo.getMaterType() + "'");
		}

		ql.append("   ORDER BY m.mater_type, m.mater_id ");

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public PaginationSupport listSelectStoreMater(PageController pageController, StoreMaterVo storeMaterVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.mater_id, b.mater_name, b.mater_type, b.brands, b.specifications, b.unit, a.mater_number, a.sid FROM t_store_material a, t_material b, t_store d WHERE a.mater_id = b.mater_id AND a.store_house_id = d.store_house_id AND a.mater_number != 0 ");

		if (storeMaterVo.getUserId() != 0) {
			ql.append(" AND d.store_cust_id = '" + storeMaterVo.getUserId() + "' ");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public void addStorePo(StorePo po) throws GenericException {
		this.merge(po);
	}

	public void deleteStorePo(StoreVo po) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" delete StorePo o where o.storeHouseId=" + po.getStoreHouseId());
		template.executeUpdate(ql.toString());

	}

	public void deleteStorePoIn(StoreInVo po) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" delete StoreInPo o where o.storeInId=" + po.getStoreInId());
		template.executeUpdate(ql.toString());

		StringBuffer ql2 = new StringBuffer();
		ql2.append(" delete StoreInDetailedPo o where o.storeInId=" + po.getStoreInId());
		template.executeUpdate(ql2.toString());
	}

	public void deleteStorePoOut(StoreOutVo po) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" delete StoreOutPo o where o.storeOutId=" + po.getStoreOutId());
		template.executeUpdate(ql.toString());

		StringBuffer ql2 = new StringBuffer();
		ql2.append(" delete StoreOutDetailedPo o where o.storeOutId=" + po.getStoreOutId());
		template.executeUpdate(ql2.toString());
	}

	public void deleteStoreIndetail(StoreInVo po) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql2 = new StringBuffer();
		ql2.append(" delete StoreInDetailedPo o where o.storeInId=" + po.getStoreInId());
		template.executeUpdate(ql2.toString());
	}

	public void deleteStoreOutdetail(StoreOutVo po) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql2 = new StringBuffer();
		ql2.append(" delete StoreOutDetailedPo o where o.storeOutId=" + po.getStoreOutId());
		template.executeUpdate(ql2.toString());
	}

	public void deleteStoreMaterDetailPo(int sidDetail, int materId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql2 = new StringBuffer();
		ql2.append(" delete StoreMaterDetailPo o where o.sidDetail = '" + sidDetail + "' and o.materId=" + materId);
		template.executeUpdate(ql2.toString());
	}

	public void addStoreInPo(StoreInPo po) throws GenericException {
		this.persist(po);
	}

	public void updateStoreInPo(StoreInPo po) throws GenericException {
		this.merge(po);

	}

	public int minStoreOutPo(StoreOutPo po) throws GenericException {
		this.merge(po);
		List quert = this.entityManager.createNativeQuery("SELECT LAST_INSERT_ID()").getResultList();
		return Integer.valueOf(quert.get(0).toString());
	}

	public void addStoreOutPo(StoreOutPo c) throws GenericException {
		this.persist(c);
	}

	public void updateStoreOutPo(StoreOutPo c) throws GenericException {
		this.merge(c);
	}

	public void addStoreOutDetailedValuePo(StoreOutDetailedValuePo c) throws GenericException {
		this.persist(c);
	}

	public int getStoreHouseIdByCustId(String custId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  a FROM StorePo a WHERE a.storeCustId ='");
		ql.append(custId);
		ql.append("'");
		List<StorePo> list = template.find(ql.toString(), null);
		return list.get(0).getStoreHouseId();
	}

	public int getStoreHouseIdBySupId(long supId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  a FROM StorePo a WHERE a.supId ='");
		ql.append(supId);
		ql.append("'");
		List<StorePo> list = template.find(ql.toString(), null);
		return list.get(0).getStoreHouseId();
	}

	public void addStoreInDetailedPo(StoreInDetailedPo po) throws GenericException {
		this.merge(po);
	}

	public void addStoreOutDetailedPo(StoreOutDetailedPo c) throws GenericException {
		this.merge(c);

	}

	public void minStoreOutDetailedPo(StoreOutDetailedPo po) throws GenericException {
		this.merge(po);
	}

	public void addStoreMaterPo(StoreMaterPo po) throws GenericException {
		this.persist(po);
	}

	public void updateStoreMaterPo(StoreMaterPo c) throws GenericException {
		this.merge(c);
	}

	public void addStoreMaterPoPersist(StoreMaterPo c) throws GenericException {
		this.persist(c);
	}

	public void addStoreMaterDetailPo(StoreMaterDetailPo c) throws GenericException {
		this.persist(c);
	}

	public void updateStoreMaterDetailPo(StoreMaterDetailPo c) throws GenericException {
		this.merge(c);
	}

	@Override
	public List<StoreMaterPo> getStoreMaterList(String userId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreMaterPo o ,StorePo a WHERE a.storeHouseId = o.storeHouseId AND  a.storeCustId ='");
		ql.append(userId);
		ql.append("'");
		List<StoreMaterPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<StoreMaterPo> getStoreMaterListbyUserIdMaterId(int userId, int materId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreMaterPo o ,StorePo a WHERE a.storeHouseId = o.storeHouseId AND  a.storeCustId ='");
		ql.append(userId);
		ql.append("' and o.materId = '" + materId + "'");
		List<StoreMaterPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<StoreMaterPo> getStoreMaterListbyHoustIdMaterId(int houseId, int materId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreMaterPo o  WHERE   o.storeHouseId ='");
		ql.append(houseId);
		ql.append("' and o.materId = '" + materId + "'");
		List<StoreMaterPo> list = template.find(ql.toString(), null);
		return list;
	}

	@Override
	public List<StoreInDetailedPo> getStoreInDetailedPoList(String storeInId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreInDetailedPo o  WHERE o.storeInId ='");
		ql.append(storeInId);
		ql.append("'");
		List<StoreInDetailedPo> list = template.find(ql.toString(), null);
		return list;
	}

	public StoreInDetailedPo getStoreInDetailedPo(int id) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT o FROM StoreInDetailedPo o  WHERE o.id ='");
		ql.append(id);
		ql.append("'");
		List<StoreInDetailedPo> list = template.find(ql.toString(), null);
		return list.get(0);
	}

	public StoreInPo getStoreInPo(int storeInId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT o FROM StoreInPo o  WHERE o.storeInId ='");
		ql.append(storeInId);
		ql.append("'");
		List<StoreInPo> list = template.find(ql.toString(), null);
		return list.get(0);
	}

	@Override
	public List getStoreInDetailedPoListDealValue() throws GenericException {

		List list = this.entityManager
				.createNativeQuery(
						"SELECT a. id , a. store_in_id , a. mater_id , a. mater_level , a. mater_num ,a. mater_num_need , a. mater_value , b.creat_date FROM t_store_in_detailed a LEFT JOIN t_store_in b ON b. store_in_id = a. store_in_id WHERE a. mater_value IS NULL AND b.creat_date IS NOT NULL")
				.getResultList();
		return list;
	}

	public List getStoreMaterDetailPoNumber(int storeHouseId, int materId) throws GenericException {
		List list = this.entityManager.createNativeQuery(
				"SELECT SUM(a.`mater_number`) FROM t_store_material_detail a WHERE a.`store_house_id`='" + storeHouseId + "' AND a.`mater_id`='"
						+ materId + "'").getResultList();
		return list;
	}

	public List<Object[]> getRealValue(int id) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT c.mater_value_undressed * 1000 * b.mater_num FROM t_material_level_bl c , t_store_in_detailed b LEFT JOIN t_store_in a ON a.store_in_id = b.store_in_id WHERE c.mater_day = a.creat_date AND a.sup_id = c.sup_id AND c.mater_id = b.mater_Id AND c.mater_level = b.mater_level AND b.id= '"
				+ id + "'");
		List list = this.entityManager.createNativeQuery(ql.toString()).getResultList();

		return list;
	}

	public StoreInDetailedPo getStoreInDetailedPoById(int id) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreInDetailedPo o  WHERE o.id ='");
		ql.append(id);
		ql.append("'");
		List<StoreInDetailedPo> list = template.find(ql.toString(), null);
		return list.get(0);
	}

	@Override
	public List<StoreOutDetailedPo> getStoreOutDetailedPoList(String storeOutId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreOutDetailedPo o  WHERE o.storeOutId ='");
		ql.append(storeOutId);
		ql.append("'");
		List<StoreOutDetailedPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<StoreMaterDetailPo> getStoreMaterDetailPo(String ordCustId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT o FROM StoreMaterDetailPo o, StorePo a WHERE a.storeHouseId = o.storeHouseId AND a.storeCustId = '");
		ql.append(ordCustId);
		ql.append("'");
		List<StoreMaterDetailPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<StoreMaterDetailPo> getStoreMaterDetailPoByHostIdAndMaterIdAndMaterDay(int hostId, int materId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT o FROM StoreMaterDetailPo o WHERE  o.storeHouseId = '");
		ql.append(hostId);
		ql.append("' AND o.materId = '" + materId + "' ORDER BY mater_day");
		List<StoreMaterDetailPo> list = template.find(ql.toString(), null);
		return list;
	}

	public StorePo getStorePo(String userId) throws GenericException {

		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT o FROM StorePo o  WHERE o.storeCustId ='");
		ql.append(userId);
		ql.append("'");
		List<StorePo> list = template.find(ql.toString(), null);
		return list.get(0);
	}

	public List<StorePo> getStorePoByStoreName(String storeName) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StorePo o  WHERE o.storeName ='");
		ql.append(storeName);
		ql.append("'");
		List<StorePo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<StorePo> getStorePoByUserId(int userId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StorePo o  WHERE o.storeCustId ='");
		ql.append(userId);
		ql.append("'");
		List<StorePo> list = template.find(ql.toString(), null);
		return list;
	}

	public StoreInPo getStoreInPo(String storeInId) throws GenericException {

		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreInPo o  WHERE o.storeInId ='");
		ql.append(storeInId);
		ql.append("'");
		List<StoreInPo> list = template.find(ql.toString(), null);
		return list.get(0);
	}

	public StoreOutPo getStoreOutPoByStoreOutId(String storeOutId) throws GenericException {

		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreOutPo o  WHERE o.storeOutId ='");
		ql.append(storeOutId);
		ql.append("'");
		List<StoreOutPo> list = template.find(ql.toString(), null);
		return list.get(0);
	}

	public List<StoreOutPo> getStoreOutPoBySupIdAndOrdId(int supId, int ordId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT  o FROM StoreOutPo o  WHERE o.supId ='");
		ql.append(supId);
		ql.append("' and o.ordId='" + ordId + "'");
		List<StoreOutPo> list = template.find(ql.toString(), null);
		return list;
	}

	@Override
	public List<StoreOutPo> getStoreOutPoForCheckConfirm(int storeHouseId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();

		ql.append("SELECT o FROM StoreOutPo o  WHERE o.storeHouseId ='");
		ql.append(storeHouseId);
		ql.append("' and o.storeOutState=1");
		List<StoreOutPo> list = template.find(ql.toString(), null);
		return list;
	}

	@Override
	public PaginationSupport listStoreOut(PageController pageController, StoreOutVo storeOutVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.store_out_id , a.store_house_id, b.store_name, a.cust_id, c.user_name, a.remark, a.store_out_way, a.store_out_state,a.creat_date FROM t_store_out a, t_store b, t_user c  WHERE a.store_house_id = b.store_house_id AND a.cust_id = c.user_id ");
		if (storeOutVo.getCustId() != 0) {
			ql.append(" and  a.cust_id = '" + storeOutVo.getCustId() + "'");
		}
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	@Override
	public List<Object[]> computerStoreValue() throws GenericException {
		List list = this.entityManager.createNativeQuery(" ").getResultList();

		return list;
	}
}
